package com.gqp.engine;

import net.sf.sevenzipjbinding.IInArchive;
import net.sf.sevenzipjbinding.SevenZip;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;

import java.io.RandomAccessFile;

/**
 * 解压rar，支持5兼容4
 */
public class UnRar implements UnCompress{
    public static void main(String[] args) {
        unRar("C:\\Users\\86166\\Desktop\\rr\\测试.rar", "C:\\Users\\86166\\Desktop\\rar\\测试");
        System.out.println("rar解压完毕");
    }

    private String inputFile;
    private String destDirPath;

    public UnRar(String inputFile, String destDirPath) {
        this.inputFile = inputFile;
        this.destDirPath = destDirPath;
    }

    @Override
    public void unCompress() {
        unRar(inputFile, destDirPath);
    }

    public static void unRar(String inputFile, String destDirPath) {
        try {
            RandomAccessFile randomAccessFile = null;
            IInArchive inArchive = null;

            // 第一个参数是需要解压的压缩包路径，第二个参数参考JdkAPI文档的RandomAccessFile
            randomAccessFile = new RandomAccessFile(inputFile, "r");
            inArchive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile));

            int[] in = new int[inArchive.getNumberOfItems()];
            for (int i = 0; i < in.length; i++) {
                in[i] = i;
            }
            inArchive.extract(in, false, new ExtractCallback(inArchive, "测试", destDirPath));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
